/*     */ package com.zimbra.cs.dav.service;
/*     */ 
/*     */ import com.zimbra.common.localconfig.KnownKey;
/*     */ import com.zimbra.common.localconfig.LC;
/*     */ import com.zimbra.common.service.ServiceException;
/*     */ import com.zimbra.common.util.Log;
/*     */ import com.zimbra.common.util.ZimbraLog;
/*     */ import com.zimbra.cs.dav.DavContext;
/*     */ import com.zimbra.cs.dav.DavException;
/*     */ import com.zimbra.cs.service.FileUploadServlet.Upload;
/*     */ import com.zimbra.cs.servlet.util.JettyUtil;
/*     */ import java.io.ByteArrayOutputStream;
/*     */ import java.io.IOException;
/*     */ import javax.servlet.http.HttpServletResponse;
/*     */ import org.apache.commons.httpclient.HttpMethod;
/*     */ import org.apache.commons.httpclient.methods.ByteArrayRequestEntity;
/*     */ import org.apache.commons.httpclient.methods.GetMethod;
/*     */ import org.apache.commons.httpclient.methods.InputStreamRequestEntity;
/*     */ import org.apache.commons.httpclient.methods.PostMethod;
/*     */ import org.apache.commons.httpclient.methods.RequestEntity;
/*     */ import org.dom4j.io.XMLWriter;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public abstract class DavMethod
/*     */ {
/*     */   protected static final int STATUS_OK = 200;
/*     */   
/*     */   public abstract String getName();
/*     */   
/*     */   public abstract void handle(DavContext paramDavContext)
/*     */     throws DavException, IOException, ServiceException;
/*     */   
/*     */   public void checkPrecondition(DavContext ctxt)
/*     */     throws DavException, ServiceException
/*     */   {}
/*     */   
/*     */   public void checkPostcondition(DavContext ctxt)
/*     */     throws DavException, ServiceException
/*     */   {}
/*     */   
/*     */   public String toString()
/*     */   {
/*  58 */     return "DAV method " + getName();
/*     */   }
/*     */   
/*     */   public String getMethodName() {
/*  62 */     return getName();
/*     */   }
/*     */   
/*     */   protected void sendResponse(DavContext ctxt)
/*     */     throws IOException
/*     */   {
/*  68 */     if (ctxt.isResponseSent())
/*  69 */       return;
/*  70 */     HttpServletResponse resp = ctxt.getResponse();
/*  71 */     resp.setStatus(ctxt.getStatus());
/*  72 */     String compliance = ctxt.getDavCompliance();
/*  73 */     if (compliance != null)
/*  74 */       setResponseHeader(resp, "DAV", compliance);
/*  75 */     if (ctxt.hasResponseMessage()) {
/*  76 */       resp.setContentType("text/xml; charset=\"UTF-8\"");
/*  77 */       DavResponse respMsg = ctxt.getDavResponse();
/*  78 */       respMsg.writeTo(resp.getOutputStream());
/*     */     }
/*  80 */     ctxt.responseSent();
/*     */   }
/*     */   
/*     */   public static void setResponseHeader(HttpServletResponse resp, String name, String value) {
/*  84 */     while (value != null) {
/*  85 */       String val = value;
/*  86 */       if (value.length() > 70) {
/*  87 */         int index = value.lastIndexOf(',', 70);
/*  88 */         if (index == -1) {
/*  89 */           ZimbraLog.dav.warn("header value is too long for %s : %s", new Object[] { name, value });
/*  90 */           return;
/*     */         }
/*  92 */         val = value.substring(0, index);
/*  93 */         value = value.substring(index + 1).trim();
/*     */       } else {
/*  95 */         value = null;
/*     */       }
/*  97 */       resp.addHeader(name, val);
/*     */     }
/*     */   }
/*     */   
/*     */   public HttpMethod toHttpMethod(DavContext ctxt, String targetUrl) throws IOException, DavException {
/* 102 */     if ((ctxt.getUpload() != null) && (ctxt.getUpload().getSize() > 0L)) {
/* 103 */       PostMethod method = new PostMethod(targetUrl)
/*     */       {
/* 105 */         public String getName() { return DavMethod.this.getMethodName(); } };
/*     */       RequestEntity reqEntry;
/*     */       RequestEntity reqEntry;
/* 108 */       if (ctxt.hasRequestMessage()) {
/* 109 */         ByteArrayOutputStream baos = new ByteArrayOutputStream();
/* 110 */         XMLWriter writer = new XMLWriter(baos);
/* 111 */         writer.write(ctxt.getRequestMessage());
/* 112 */         reqEntry = new ByteArrayRequestEntity(baos.toByteArray());
/*     */       } else {
/* 114 */         reqEntry = new InputStreamRequestEntity(ctxt.getUpload().getInputStream(), ctxt.getUpload().getSize());
/*     */       }
/* 116 */       method.setRequestEntity(reqEntry);
/* 117 */       return method;
/*     */     }
/* 119 */     new GetMethod(targetUrl) {
/*     */       public String getName() {
/* 121 */         return DavMethod.this.getMethodName();
/*     */       }
/*     */     };
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   protected void disableJettyTimeout(DavContext context)
/*     */     throws IOException
/*     */   {
/* 139 */     long maxIdleTime = LC.zimbra_dav_max_idle_time_ms.intValue();
/* 140 */     JettyUtil.setIdleTimeout(maxIdleTime, context.getRequest());
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/cs/dav/service/DavMethod.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */